<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!-- MapperXml：${tableInfo.table_comment?if_exists}. @author ${ftl_author} @since ${ftl_now} @version ${ftl_version}--> 
<#assign tableKeyName=idColumn>
<#assign beanKeyName=idFiled>
<#assign beanKeyType=idType> 
<mapper namespace="${ftl_dao_package}.I${className}Mapper" >
    <resultMap id="${className}Map" type="${ftl_domain_package}.${className}">
        <#list classFields as v>
        <result property="${v.field}" column="${v.columnName}"/>
        </#list>
    </resultMap>

    <insert id="insert" parameterType="${ftl_domain_package}.${className}">
        <#if ftl_dbType="MYSQL">
        <selectKey keyProperty="${beanKeyName}" resultType="${beanKeyType}" order="AFTER">
            SELECT LAST_INSERT_ID() AS ${beanKeyName}
        </selectKey>
        <#elseif ftl_dbType="POSTGRESQL">
        <selectKey keyProperty="${beanKeyName}" resultType="${beanKeyType}" order="BEFORE">
            SELECT nextval('${tableName}_${tableKeyName}_seq')
        </selectKey>
        </#if>
        INSERT INTO ${tableName} (<trim suffixOverrides=",">
            <#list classFields as v>
			<#if v.is_nullable="YES">
            <if test="${v.field} != null">${v.columnName},</if>
            <#else>
            ${v.columnName},
            </#if>
            </#list>
        </trim>)VALUES(<trim suffixOverrides=",">
            <#list classFields as v>
            <#if v.is_nullable="YES">
            <if test="${v.field} != null"> ${"#{"+v.field+"}"},</if>
            <#else>
            ${"#{"+v.field+"}"},
            </#if>
            </#list>
        </trim>)
    </insert>

    <update id="updateById" parameterType="${ftl_domain_package}.${className}">
        update ${tableName}
        <trim prefix="SET" suffixOverrides=",">
        <#list classFields as v>
        	<if test="${v.field} != null">${v.columnName} = ${"#{"+v.field+"}"},</if>
        </#list>
        </trim>
        where ${tableKeyName} = ${"#{"+beanKeyName+"}"}
    </update>

    <delete id="deleteById" parameterType="int">
        delete from ${tableName} where ${tableKeyName} = ${"#{"+beanKeyName+"}"}
    </delete>

    <select id="getById" parameterType="int" resultMap="${className}Map">
        select * from ${tableName} where ${tableKeyName} = ${"#{"+beanKeyName+"}"}
    </select>

    <select id="getListByCondition" resultMap="${className}Map" parameterType="${ftl_domain_package}.${className}">
        select * from ${tableName}
        <include refid="select_where_clause"/>
        order by ${tableKeyName} desc
    </select>

    <sql id="select_where_clause">
        <trim prefix="where" prefixOverrides="AND|OR">
        <#list classFields as v>
        	<if test="obj.${v.field} != null">AND ${v.columnName} = ${"#{"+v.field+"}"},</if>
        </#list>
        </trim>
    </sql>
    
     <select id="getListPage" resultMap="${className}Map">
        select * from ${tableName}
        <include refid="page_where_clause"/>
        order by ${tableKeyName} desc
        <if test="page!=null">
            LIMIT ${"#"}{page.pageSize} OFFSET ${"#"}{page.start}
        </if>
    </select>

    <select id="count" resultType="int">
        select count(*) from ${tableName}
        <include refid="page_where_clause"/>
    </select>

    <sql id="page_where_clause">
        <trim prefix="where" prefixOverrides="AND|OR">
        <#list classFields as v>
            <if test="obj.${v.field} != null">AND ${v.columnName} = ${"#{"+v.field+"}"},</if>
        </#list>
        <!-- otherParam -->
        <if test="otherParam != null">
            <!--your script-->
        </if>
        </trim>
    </sql>
</mapper>